package com.wuseguang.springcloud.dao.jdbc;

import com.wuseguang.springcloud.JdbcTemplate.BaseTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class RolesJdbc extends BaseTemplate {


    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 查询角色功能权限
     *
     * @param roleId 角色id
     * @return
     */
    public List<Map<String, Object>> findRoleFunction(Integer roleId, String schoolId) {


        //查询功能权限
        String sql = "select id,name,pid ,if(count(1)>1,1,0) as state from ( "
                + "  select  jurisdiction_id id,jurisdiction_name name , jurisdiction_pid pid   from school_jurisdiction where  jurisdiction_pid =? AND school_id = ? AND web=1 UNION " +
                "select  jurisdiction_id id,jurisdiction_name name , jurisdiction_pid pid   from school_jurisdiction where  jurisdiction_pid =? AND school_id = ? AND tapplet=1 UNION ALL"
                + "  SELECT g.jurisdiction_id  id ,g.jurisdiction_name name,g.jurisdiction_pid pid FROM grouping_jurisdiction g,school_jurisdiction s WHERE g.grouping_id = ?  AND s.jurisdiction_id=g.jurisdiction_id  AND g.jurisdiction_pid=s.jurisdiction_pid  AND g.school_id=s.school_id AND  g.jurisdiction_pid =?  AND  g.school_id =?"
                + "  ) jurisdiction group by id  ";

        logger.info("管理员查询功能权限 参数1 = {}，参数2 = {},参数3 = {},参数 = {}", 0, schoolId, roleId, 0, schoolId);

        List<Map<String, Object>> list = queryListMap(sql, 0, schoolId, 0, schoolId, roleId, 0, schoolId);

        //循环一级权限
        for (Map<String, Object> s : list) {
            List<Map<String, Object>> twoList = queryListMap(sql, s.get("id"), schoolId, s.get("id"), schoolId, roleId, s.get("id"), schoolId);
            //循环二级权限
            for (Map<String, Object> twos : twoList) {
                List<Map<String, Object>> ThreeList = queryListMap(sql, twos.get("id"), schoolId, twos.get("id"), schoolId, roleId, twos.get("id"), schoolId);
                twos.put("list", ThreeList);
            }
            s.put("list", twoList);
        }

        return list;
    }

}
